package com.jy.rhin.facade.ws.zhuanzhen;

import cn.org.chiss.rhin._2015.base.*;
import cn.org.chiss.rhin._2015.base.document.Document;
import cn.org.chiss.rhin._2015.base.object.*;
import cn.org.chiss.rhin._2015.base.zhuanzhen.*;
import com.jy.rhin.config.NamespaceConstants;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import com.jy.rhin.support.aspect.AuditLog;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.lang.String;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * This class was generated by Apache CXF 3.5.1
 * 2024-04-10T15:54:24.031+08:00
 * Generated source version: 3.5.1
 *
 */
@WebService(
        serviceName = "TwoWayReferral",
        portName = "TwoWayReferralPort",
        targetNamespace = NamespaceConstants.NS_RHIN,
        endpointInterface = "com.jy.rhin.facade.ws.zhuanzhen.TwoWayReferral")
@Component
public class TwoWayReferralPortImpl implements TwoWayReferral{

    @Override
    @AuditLog
    public TwoWayReferralApproveQueryResponseType twoWayReferralApproveQuery(TwoWayReferralApproveQuery query) {
        if("202311141029271815.150680685968055979812241403506052511"
                .equals(query.getAdhocQuery().getSlot().get(0).getValueList().getValue().get(0).getValue())){
            //模拟反例
            throw new RhinWSException("TwoWayReferralQueryResponseFault","查询异常");
        }
        TwoWayReferralApproveQueryResponseType responseType = new TwoWayReferralApproveQueryResponseType();
        List<ReferralApproveType> list = new ArrayList<>();
        ReferralApproveType r = new ReferralApproveType();
        r.setIdentifier(new Identifier().setValue(new ID().setValue("1236543")).setSystem(new URI().setValue("2.16.156.10011.15")));
        r.setReferralID(new ID().setValue("a4798bb3f4a8476f9605a531e47c9b8d"));
        r.setReferralDepartment(getReferralDepartment());
        r.setValidPeriod(getValidPeriod());
        r.setAuthor(getDoctor());
        r.setDateTime(new DateTime().setValue("2023-11-09 15:32:19"));
        r.setReceiveDoctor(getDoctor());
        list.add(r);
        responseType.setReferralApprove(list);
        return responseType;
    }

    @Override
    @AuditLog
    public ObjectRef4Req twoWayReferralRequest(ReferralType2 referral) {
        if("123"
                .equals(referral.getReferral().getId().getValue())){
            //模拟反例
            throw new RhinWSException("TwoWayReferralQueryResponseFault","异常");
        }
        MainID id = new MainID();
        id.setValue("123");
        id.setSystem("1");
        return new ObjectRef4Req().setReferralRef(new ObjectRef().setId(id));
    }

    @Override
    @AuditLog
    public ObjectRef4Approve twoWayReferralApprove(ReferralApproveType2 referralApprove) {
        if("123"
                .equals(referralApprove.getReferralApprove().getReferralID().getValue())){
            //模拟反例
            throw new RhinWSException("TwoWayReferralQueryResponseFault","异常");
        }
        MainID id = new MainID();
        id.setValue("123");
        id.setSystem("1");
        return new ObjectRef4Approve().setReferralRef(new ObjectRef().setId(id));
    }

    @Override
    @AuditLog
    public TwoWayReferralQueryResponseType twoWayReferralQuery(TwoWayReferralQuery query) {
        if("202311090339402105.175660335574963724499046785460579831"
                .equals(query.getAdhocQuery().getSlot().get(0).getValueList().getValue().get(0).getValue())){
            //模拟反例
            throw new RhinWSException("TwoWayReferralQueryResponseFault","查询异常");
        }

        List<ReferralType> referral  =new ArrayList<>();
        ReferralType r = new ReferralType();
        MainID id = new MainID();
        id.setValue("1035114");
        id.setSystem("2.16.156.10011.0");
        r.setId(id);
        r.setStatusCode(new ObjectStatus().setValue(ObjectStatusPrimitive.ACTIVE));
        r.setClassCode(new CodeableConcept().setCoding(getCode("1","门诊",
                "2.16.156.10011.1","转诊单分类字典")).setText(new cn.org.chiss.rhin._2015.base.String().setValue("门诊")));
        r.setIdentifier(new Identifier().setValue(new ID().setValue("1711940734118469205")).setSystem(new URI().setValue("2.16.156.10011.12")));
        r.setReferralToward(new ReferralTowardType().setValue( ReferralTowardPrimitive.UP));
        r.setPatient(getPatient());
        r.setReferralDepartment(getReferralDepartment2());
        r.setReferralDoctor(getDoctor());
        List<OrganizationBrief> receiveDepartment = new ArrayList();
        receiveDepartment.add(getReferralDepartment());
        r.setReceiveDepartment(receiveDepartment);
        List<PractitionerBrief> receiveDoctor = new ArrayList();
        receiveDoctor.add(getDoctor());
        r.setReceiveDoctor(receiveDoctor);
        r.setReferralReason(getCodeableConcepts("1","专科诊治",
                "2.16.156.10011.1","转诊理由编码"));
        r.setHealthConditon(getCodeableConcepts("I10.x09","颈椎病",
                "2.16.156.10011","诊断代码表(ICD-10)"));
        r.setValidPeriod(getValidPeriod());
        r.setDateTime(new DateTime().setValue("20240402080000"));
        r.setReferralDateTime(new DateTime().setValue("20240402080000"));
        List<Document> documents =new ArrayList<>();
        documents.add(new Document().setContent("cid1083085045031".getBytes()).setId(new URI().setValue("123")));
        r.setReferralDocument(documents);
        referral.add(r);
        return new TwoWayReferralQueryResponseType().setReferral(referral);
    }

    private Period getValidPeriod(){
       return new Period().setStart(new DateTime().setValue("20240402080000"))
                .setEnd(new DateTime().setValue("20240402083000"))
                .setDuration(new Duration().setValue(new BigInteger("1")));
    }

    private List<CodeableConcept> getCodeableConcepts(String value,String display,String system,String systemName ){
        List<CodeableConcept> list = new ArrayList<>();
        CodeableConcept c = new CodeableConcept().setCoding(getCode(value,display,system,systemName));
        list.add(c);
        return list;
    }

    private PractitionerBrief getDoctor(){
        return new PractitionerBrief().setIdentifier(getIdentifier("106000025","2.16.156.10011.8"))
                .setName(new HumanName().setValue("熊红帅"))
                .setDuty(getCode("1","医生","2.16.156.10011.2","医生职务字典"))
                .setProfessionalTitle(getCode("1","中级","2.16.156.10011.1","医生职称字典"));
    }

    private OrganizationBrief getReferralDepartment(){
        OrganizationBrief o = new OrganizationBrief().setIdentifier(getIdentifier("NS0403.0210","2.16.156.10011.8"))
                .setClassCode(getCode("1","门诊","2.16.156.10011.81","科室分类"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("脊柱外科门诊"))
                .setPartOf(getPartOf());
        return o;
    }
    private OrganizationBrief getPartOf(){
        return new OrganizationBrief().setIdentifier(getIdentifier("455848964","2.16.156.10011.8"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("华中科技大学协和深圳医院"))
                .setTelecom(new Telecom().setValue(new cn.org.chiss.rhin._2015.base.String().setValue("0755-26510696")));
    }

    private OrganizationBrief getReferralDepartment2(){
        OrganizationBrief o = new OrganizationBrief().setIdentifier(getIdentifier("KS001","2.16.156.10011.8"))
                .setClassCode(getCode("1","门诊","2.16.156.10011.81","科室分类"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("儿科"))
                .setPartOf(getPartOf2());
        return o;
    }
    private OrganizationBrief getPartOf2(){
        return new OrganizationBrief().setIdentifier(getIdentifier("PDY550070","2.16.156.10011.8"))
                .setName(new cn.org.chiss.rhin._2015.base.String().setValue("深圳市南山区医疗集团总部莲城社区健康服务中心"))
                .setTelecom(new Telecom().setValue(new cn.org.chiss.rhin._2015.base.String().setValue("0755-26510696")));
    }

    private List<Coding> getCode(String value,String display,String system,String systemName){
        List<Coding> coding = new ArrayList<>();
        coding.add(new Coding().setCode(new Code().setValue(value))
                .setDisplay(new cn.org.chiss.rhin._2015.base.String().setValue(display))
                .setSystem(new URI().setValue(system))
                .setSystemName(new cn.org.chiss.rhin._2015.base.String().setValue(systemName))
                .setVersion(new cn.org.chiss.rhin._2015.base.String().setValue("1.0")));
        return coding;
    }

    private PersonBrief getPatient(){
        return new PersonBrief().setIdentifier(getIdentifier("360421199308280042","2.16.156.10011.1.3"))
                .setName(new HumanName().setValue("刘以秋"))
                .setBirthTime(new Date().setValue("19930828"))
                .setGender(new Code().setValue("2"))
                .setAddr(new Address().setText(new cn.org.chiss.rhin._2015.base.String().setValue("中山南街二坊43-1号702")))
                .setTelecom(new Telecom().setValue(new cn.org.chiss.rhin._2015.base.String().setValue("17727587774")));
    }

    private  List<Identifier> getIdentifier(String value, String system){
        List<Identifier> list = new ArrayList<>();
        list.add(new Identifier().setValue(new ID().setValue(value)).
                setSystem(new URI().setValue(system)));
        return list;
    }


    @Override
    @AuditLog
    public ObjectRef4Abort twoWayReferralAbort(TwoWayReferralAbortType referralRef) {
        if("123"
                .equals(referralRef.getReferralRef().getId().getValue())){
            //模拟反例
            throw new RhinWSException("TwoWayReferralQueryResponseFault","异常");
        }
        MainID id = new MainID();
        id.setValue("123");
        id.setSystem("1");
        return new ObjectRef4Abort().setReferralRef(new ObjectRef().setId(id));
    }
}
